﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using EHR.Views;
using EHR.Common;
using CommunitySystem.SystemService;

namespace CommunitySystem
{
    public partial class MainPage : UserControl
    {
       
        public MainPage()
        {
            InitializeComponent();
            Worker.MainPage = this;
            Worker.BusyGrid = busyGrid;
            Worker.Indicator = busy;
            //yanyan 分别调用有或无ikey的登陆界面
            if (Session.IsIKey)
            {
                LoginIKey.LoginCompleted += new EventHandler<LoginCompletedArgs>(Login_LoginCompleted);
            }
            else
            {
                Login.LoginCompleted += new EventHandler<LoginCompletedArgs>(Login_LoginCompleted);
            }

            MasterPage.Logout += new EventHandler(MasterPage_Logout); // Emily

            //IndexControl.Logout += new EventHandler(PageIndex_Logout);
        }

        void PageIndex_Logout(object sender, EventArgs e)
        {
            InintLoad();
        }
        private void InintLoad()
        {
            SaveLogoutSession();
            appGrid.Children.Clear();
            loginGrid.Children.Clear();
            //yanyan 分别调用有或无ikey的登陆界面
            if (Session.IsIKey)
            {
                loginGrid.Children.Add(new LoginIKey());
            }
            else
            {
                loginGrid.Children.Add(new Login());
            }
        }
        void MasterPage_Logout(object sender, EventArgs e)
        {
            InintLoad();


        }
        void Login_LoginCompleted(object sender, LoginCompletedArgs e)
        {
            SaveLoginSession(e.User);
            if (appGrid.Children.Count == 0)//第一次登录
            {
                appGrid.Children.Clear();
                appGrid.Children.Add(new HomePage());  //Emily
                //appGrid.Children.Add(new HomePageIndex());
            }

            loginGrid.Children.Clear();
        }



      
        private void LayoutRoot_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }

        private void LayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
          
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //loginGrid.Children.Add(new Login());   
            //yanyan 分别调用有或无ikey的登陆界面
            if (Session.IsIKey)
            {
                loginGrid.Children.Add(new LoginIKey());
            }
            else
            {
                loginGrid.Children.Add(new Login()); 
            }
        }
        private void SaveLoginSession(UserInfo user)
        {
            Session.LoginUser = user;
        }

        private void SaveLogoutSession()
        {
            Session.Logout();
        }
    }
}
